package com.leetone;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

/**
* @Title: LoginServlet.java
* @Package 
* @Description: 【登录】
* @date 2018年2月8日 下午2:31:54
* @version V1.0
* @Company: 上海
* @author:[谭磊][leetone123@163.com]
*/

/**
* @ClassName: LoginServlet
* @Description: 【】
* @date 2018年2月8日 下午2:31:54
* @Company: 上海
* @author:[谭磊][leetone123@163.com]
*/

public class LoginServlet extends HttpServlet{

	
	/**
	* @Fields field:field:【】
	*/
	
	private static final long serialVersionUID = 1L;
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("你来到登录啦！");
		req.getRequestDispatcher("login.jsp").forward(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("开始提交登录信息。。。");
		String userName = req.getParameter("userName");
		String password = req.getParameter("password");
		Subject currentUser =SecurityUtils.getSubject();
		//创建令牌
		UsernamePasswordToken token = new UsernamePasswordToken(userName, password);
		try {
			currentUser.login(token);
			Session session =currentUser.getSession();
			System.out.println("sessionId:"+session.getId());
			System.out.println("用户IP:"+session.getHost());
			System.out.println("session过期时间："+session.getTimeout());
			session.setAttribute("user", userName);
			req.getRequestDispatcher("index.jsp").forward(req, resp);
		}catch(AuthenticationException e)
		{
			e.printStackTrace();
			System.out.println("登录失败");
		}
	}
}
